<?php

namespace Kollway\CodeGenerator\Generators;

class ListViewGenerator extends Generator
{
    public function generateListViewString()
    {
        $toolbar_string = $this->getToolBarString();
        $table_header_string = $this->getTableHeaderString();
        $tbody_string = $this->getTableBodyString();
        $pagination_string = $this->getPaginationString();
        $modal_form_string = $this->getModalFormString();
        $section_script_string = $this->getSectionScriptString();

        $list_view_string = <<<LIST
@extends('public.base')

@section('main-content')
    <div class="row">
        <div class="col-md-12">
            <div class="list-title">{$this->list_name}列表</div>
        </div>
    </div>
    
$toolbar_string

    <div class="row">
        <div class="col-md-12">
            <table class="table table-bordered table-striped">
$table_header_string

$tbody_string
            </table>
        </div>
    </div>

$pagination_string

$modal_form_string
@stop

$section_script_string
LIST;
        $this->content = $list_view_string;

        return $this->content;
    }

    // 获取按钮及搜索部分
    private function getToolBarString()
    {
        $buttons = $this->getToolBarButtonsString();

        $toolbar_string = <<<TOOLBAR
    <div class="row list-toolbar">
        <div class="col-lg-4">
{$buttons}
        </div>\n
TOOLBAR;
        if ($this->search_fields) {
            $toolbar_string .= <<<LIST
        @include('components.search', ['url' => \Route::currentRouteName(), 'placeholder' => '$this->search_placeholder'])\n
LIST;
        }

        $toolbar_string .= <<<TOOLBAR
    </div>
TOOLBAR;

        return $toolbar_string;
    }

    // 获取 新增、删除、启用、禁用 按钮
    private function getToolBarButtonsString()
    {
        $new_button = <<<HTML
            <a class="btn btn-default" href="{{build_action('Admin\\{$this->model_name}Controller@getForm')}}">新 增</a>
HTML;
        // 对话框方式新增编辑
        if ($this->form_type == 'dialog') {
            $new_button = <<<HTML
            <a class="btn btn-default" onclick="add{$this->model_name}()">新增{$this->form_title}</a>
HTML;
        }

        $string = <<<HTML
{$new_button}
            <button class="btn btn-default ajax-post confirm" url="{{route('status', ['mod' => '$this->table', 'status' => 1])}}" target-form="ids">启 用</button>
            <button class="btn btn-default ajax-post confirm" url="{{route('status', ['mod' => '$this->table', 'status' => 0])}}" target-form="ids">禁 用</button>
            <button class="btn btn-default ajax-post confirm" url="{{build_action('Admin\\{$this->model_name}Controller@postDelete', ['mod' => '$this->table'])}}" target-form="ids">删 除</button>
HTML;

        return $string;
    }

    // 获取表头部分
    private function getTableHeaderString()
    {
        $string = <<<HTML
                <thead>
                <tr>
                    <th width="60">
                        <div class="checkbox checkbox-replace neon-cb-replacement" id="check-all">
                            <label class="cb-wrapper">
                                <input type="checkbox" class="check-all">
                                <div class="checked"></div>
                            </label>
                        </div>
                    </th>
                    <th>ID</th>\n
HTML;
        // 表头
        foreach ($this->column_info as $column) {
            if (!in_array($column['field'], ['id', 'status', 'is_deleted', 'create_time', 'update_time'])) {
                $string .= <<<LIST
                    <th>{$column['comment']}</th>\n
LIST;
            }
        }

        $string .= <<<HTML
                    <th>状态</th>
                    <th width="100">操作</th>
                </tr>
                </thead>
HTML;

        return $string;
    }

    // 获取表格内容部分
    private function getTableBodyString()
    {
        $buttons = $this->getTrButtonsString();

        $string = <<<LIST
                <tbody>
                @forelse(\$list as \$key => \$val)
                    <tr>
                        <td>
                            <div class="checkbox checkbox-replace ids neon-cb-replacement">
                                <label class="cb-wrapper">
                                    <input name="id[]" type="checkbox" value="{{\$val['id']}}" autocomplete="off">
                                    <div class="checked"></div>
                                </label>
                            </div>
                        </td>
                        <td>{{\$val['id']}}</td>\n
LIST;
        // 单元格
        foreach ($this->column_info as $column) {
            if (!in_array($column['field'], ['id', 'status', 'is_deleted', 'create_time', 'update_time'])) {
                $string .= <<<LIST
                        <td>{{\$val['{$column['field']}']}}</td>\n
LIST;
            }
        }

        $string .= <<<LIST
                        <td>
                            <a href="javascript:"
                               data-url="{{route('status')}}"
                               data-mod="account"
                               data-id="{{\$val['id']}}"
                               data-status="{{abs(1-\$val['status'])}}"
                               data-img_prefix="{{asset('admin/images')}}"
                               class="ajax-post-status">
                                @if(\$val['status'] == 1)
                                    <i class="entypo-check"></i>
                                @else
                                    <i class="entypo-cancel"></i>
                                @endif
                            </a>
                        </td>
                        <td>
$buttons
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="99" class="text-center"> aOh! 暂时还没有内容!</td></tr>
                @endforelse
                </tbody>
LIST;

        return $string;
    }

    // 获取表格内容每一行的可用按钮
    private function getTrButtonsString()
    {
        $edit_btn = <<<EDIT
                            <a href="{{build_action('Admin\\{$this->model_name}Controller@getForm', ['id' => \$val['id']])}}"
                               class="entypo-icon" {{entypo_tooltip('编辑')}}>
                                <i class="entypo-pencil"></i>
                            </a>
EDIT;
        if ($this->form_type == 'dialog') {
            $edit_btn = <<<EDIT
                            <a href="javascript: edit{$this->model_name}('{{\$val['id']}}')"
                               class="entypo-icon" {{entypo_tooltip('编辑')}}>
                                <i class="entypo-pencil"></i>
                            </a>
EDIT;
        }

        $string = <<<HTML
$edit_btn

                            <a href="{{build_action('Admin\\{$this->model_name}Controller@postDelete', ['mod' => '{$this->table}', 'id' => \$val['id']])}}"
                               class="entypo-icon entypo-icon-del confirm ajax-post-url" {{entypo_tooltip('删除')}}>
                                <i class="entypo-cancel"></i>
                            </a>
HTML;

        return $string;
    }

    // 获取分页部分
    private function getPaginationString()
    {
        $string = <<<HTML
    @if(count(\$list) != 0)
    <div class="dataTables_wrapper my_pagination">
        {!! \$list->page !!}
    </div>
    @endif
HTML;
        return $string;
    }

    // 模态表单
    private function getModalFormString()
    {
        if ($this->form_type != 'dialog') {
            return '';
        }

        $modal_id = "{$this->table}-modal";
        $modal_form_items = $this->getModalFormItemString();

        $string = <<<HTML
    <div id="{$modal_id}" class="modal fade">
        <div class="modal-dialog" style="max-width: 300px;height: 211px;">
            <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="{$modal_id}-title"></h4>
                </div>

                <form method="post" class="dialog-form">
                    <input type="hidden" name="id" value="0">
                    <input type="hidden" name="sort_index" value="999">
                    <input type="hidden" name="create_time" value="{{time()}}">
                    <input type="hidden" name="update_time" value="{{time()}}">

                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
$modal_form_items
                            </div>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-info ajax-post dialog-post" id="submit" target-form="dialog-form"
                                url="{{build_action('Admin\\{$this->model_name}Controller@postForm')}}"
                                onResponse="onResponse">确定
                        </button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
HTML;

        return $string;
    }

    // modal 表单项
    private function getModalFormItemString()
    {
        $string = '';
        // 表单输入框
        foreach ($this->column_info as $column) {
            if (!in_array($column['field'], ['id', 'status', 'sort_index', 'is_deleted', 'create_time', 'update_time'])) {
                $string .= <<<HTML
                                <div class="form-group">
                                    <label class="control-label">{$column['comment']}</label>
                                    <input type="text" name="{$column['field']}" class="form-control" placeholder="">
                                </div>\n
HTML;

            }
        }

        return $string;
    }


    // script 部分
    private function getSectionScriptString()
    {
        if ($this->form_type != 'dialog') {
            return '';
        }

        $string = <<<HTML
@section('script')
    @include("code-generator.script", [
        'list' => \$list,
        'form_title' => '{$this->form_title}',
        'table' => '{$this->table}',
        'model_name' => '{$this->model_name}',
    ])
@stop
HTML;
        return $string;
    }
}